package cc.facesign.admin.utils;

import cc.facesign.admin.bean.TblDept;
import cc.facesign.admin.bean.TblUser;
import cc.facesign.admin.bean.vo.ConfVo;
import cc.facesign.admin.service.TblConfService;
import cc.facesign.admin.service.TblDeptService;
import cc.facesign.admin.service.TblManageDeptService;
import cc.facesign.admin.service.TblUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限工具类
 */
@Component
public class PowerUtils {

    @Autowired
    private TblUserService userService;

    @Autowired
    private TblDeptService deptService;

    @Autowired
    private TblConfService confService;

    @Autowired
    private TblManageDeptService manageDeptService;

    /**
     * 获取当前用户id
     * @return
     */
    public TblUser getCurrentUser() {
        return userService.getCurrent();
    }

    public String getId() {
        return getCurrentUser().getId();
    }

    /**
     * 判断是否为admin用户
     */
    public boolean isAdmin() {
        return userService.getCurrent().getId().equals("0");
    }

    /**
     * 获取当前用户的部门id
     */
    public String getCurrentDeptId() {
        TblUser current = userService.getCurrent();
        return current.getDeptId();
    }


    /**
     * 获取当前用户的root部门id
     */
    public String getRid() {
        TblUser user = userService.getCurrent();
        TblDept dept = deptService.getById(user.getDeptId());
        TblDept root = deptService.getById(dept.getRid());
        return root.getId();
    }

    /**
     * 获取当前用户所管理的所有部门id（包括子部门）
     */
    public HashSet<String> getCurrentChildDeptIdSet() {
        String userId = getCurrentUser().getId();
        return userService.getAllManageDeptIdSet(userId);
    }

    /**
     * 查当前用户所能管理的会议id
     * 包括当前用户创建的会议id
     */
    public HashSet<String> getCurrentChildConfIdSet() {
        HashSet<String> deptSet = getCurrentChildDeptIdSet();


        HashSet<String> set = new HashSet<>();
        if (deptSet.size() != 0) { // 如果当前用户是管理员
            set = confService.getListByDeptIdBatch(new ArrayList<>(deptSet)).stream()
                    .map(ConfVo::getId)
                    .collect(Collectors.toCollection(HashSet::new));
        }

        // 也添加上创建的会议
        List<String> collect = userService.getCreateConfList(getId()).stream()
                .map(x -> x.getId())
                .collect(Collectors.toList());
        set.addAll(collect);

        return set;
    }

    /**
     * 当前用户是否是会议的创建者
     */
    public boolean isConfCreater(String confId) {
        String id = getCurrentUser().getId();
        return confService.getById(confId).getCreaterId().equals(id);
    }

    /**
     * 当前用户是否可管理指定用户
     */
    public boolean isMaster(String userId) {
        TblUser user = userService.getById(userId);

        // 是否为管理上级
        boolean flag = getCurrentChildDeptIdSet().contains(user.getDeptId());
        // 如果用户是管理者
        if (userService.countManageForDept(userId) != 0) {
            // 判断是否是同级
            if (getCurrentDeptId().equals(user.getDeptId())) {
                flag = false;
            }
        }

        return flag;
    }
}
